// *** PLAYER OBJECT *** //
// Controls the different actions for the player
//
// @project: GoldenShield
// @author: Mathias Sørensen
// @version: 1.0

var player = {
    pxMove: 4,   //pixels per movent
    height: 55,
    width: 40,
    backgroundImg: "url('img/player/aspisheronew2.png')",    //picture for the main player
    x: 0,    //position for the player
    y: 0,    //position for the player
    health: 5,   //starting health
    animatePx: 30,
    WhatImage: 1,
    jewels: 0,   //jewels in possession
    playerCreated: false,
    debugMode: false,
    hit: false,
    playerWay: null,
    bgYPos: null,
    playerKills: 0,
    mapKills: 0,
    surfaceSound: null,
    mapSurfaceSound: "walk",

    //creates the character including position
    create: function(x,y){
        if(!this.playerCreated) {
            this.x = x;
            this.y = y;
            this.playerWay = "down";
            $('#scene').append('<div id="player"></div>');
            $('#player').css({
                width: this.width + 'px', 
                border: '0px solid black', 
                height: this.height + 'px', 
                backgroundImage: this.backgroundImg, 
                position: "absolute", 
                top: this.y + "px", 
                left: this.x + "px", 
                zIndex: 99, 
                backgroundPosition: "0 -24px"
            })
            this.playerCreated = true;
        }
    },

    //Resets the object back to defualt values
    reset: function() {
        this.pxMove = 4;
        this.height = 55;
        this.width = 40;
        this.backgroundImage = "url('img/player/aspisheronew2.png')";
        this.x = 0;
        this.y = 0;
        this.health = 5;
        this.animatePx = 30;
        this.WhatImage = 1;
        this.jewels = 0;
        this.playerCreated = false;
        this.debugMode = false;
        this.hit = false;
        this.playerWay = null;
        this.bgYPos = null;
        this.playerKills = 0;
        this.mapKills = 0;
    },

    //moves the character from one point to another without appending a new diw
    move: function(x,y) {
        this.x = x;
        this.y = y;
        $('#player').css({
            left: x + 'px', 
            top: y + 'px'
            });
    },

    //moves the character to the left
    moveLeft: function(){
        this.x -= this.pxMove;
        this.makeMove();
        this.animateCharLeft();
        this.playerWay = "left";

    },

    //moves the charecter to the right
    moveRight: function(){
        this.x += this.pxMove;
        this.makeMove();
        this.animateCharRight();
        this.playerWay = "right";
    },

    //moves the character up
    moveUp: function(){
        this.y -= this.pxMove;
        this.makeMove();
        this.animateCharUp();
        this.playerWay = "up";
    },

    //moves the charecter down
    moveDown: function(){
        this.y += this.pxMove;
        this.makeMove();
        this.animateCharDown();
        this.playerWay = "down";
    },

    makeMove: function() {
        
        if(this.surfaceSound != null) {
            sound.play(this.surfaceSound);
        } else if(this.mapSurfaceSound) {
            sound.play(this.mapSurfaceSound);
        }
        
        $("#player").css({
            top: this.y + 'px', 
            left: this.x + 'px'
            });
    },

    //animates character when walking upwards
    animateCharUp: function() {
        var moveBg = -367;
        if(this.WhatImage == 1) {
            this.WhatImage = 2;
            moveBg += this.animatePx
        } else {
            this.WhatImage = 1;
            moveBg -= this.animatePx
        }
        $('#player').css({
            backgroundPosition: moveBg + "px -24px"
        })
    },

    //animates character when walking downwards
    animateCharDown: function() {
        var moveBg = -30;
        if(this.WhatImage == 1) {
            this.WhatImage = 2;
            moveBg += this.animatePx
        } else {
            this.WhatImage = 1;
            moveBg -= this.animatePx
        }
        $('#player').css({
            backgroundPosition: moveBg + "px -24px"
        })
    },

    //animates character when walking right
    animateCharRight: function() {
        var moveBg = -255;
        if(this.WhatImage == 1) {
            this.WhatImage = 2;
            moveBg += this.animatePx
        } else {
            this.WhatImage = 1;
            moveBg -= this.animatePx
        }
        $('#player').css({
            backgroundPosition: moveBg + "px -24px"
        })
    },

    //animates character when walking left
    animateCharLeft: function() {
        var moveBg = -140;
        if(this.WhatImage == 1) {
            this.WhatImage = 2;
            moveBg += this.animatePx
        } else {
            this.WhatImage = 1;
            moveBg -= this.animatePx
        }
        $('#player').css({
            backgroundPosition: moveBg + "px -24px"
        })
    },

    //adds health to player
    addHealth: function() {
        if(this.health < 5) {
            this.health = this.health + 1;
        }
    },

    //remove health from player
    removeHealth: function() {
        if(this.health > 0 && !this.debugMode) {
            this.health = this.health -1;
            scene.onHitAnimation();
        }
    },

    //kills character and fades him out
    die: function() {
        if(!this.debugMode) {
            $('#player').fadeOut(function() {
                $(this).remove();
            });
        }
    },

    //adds jewels to the character
    addJewels: function() {
        if(this.jewels < 9){
            this.jewels = this.jewels + 1;
        }
    },

    //removes jewels from the character
    removeJewels: function() {
        if(this.jewels > 0) {
            this.jewels = this.jewels -1;
        }
    },

    //runs with the main loop and updates health
    update: function() {
        if(this.health == 0){
            this.die();
        }
    },

    //Updates the character div size
    updateSize: function(width,height) {
        this.width = width;
        this.height = height;
        $('#player').css({
            width: width + "px", 
            height: height + "px"
            });
    },

    //The Hit Function
    hitEnemy: function() {
        if(!this.hit) {
            this.hit = true;
            this.animateHit(true);

            var hitObject = collision.collisionObject;
            if(hitObject) {
                var id = $(hitObject).attr('id');
                character.damage(id);
            }

            var func = function() {
                if(player.hit) {
                    player.hit = false;
                    player.animateHit(false);
                }
            }
            setTimeout(func, 120);
        }
    },

    //Sword animation
    animateHit: function(hit) {
        var moveBg;
        var moveBgY;
        var width;
        var height;
        var movePlayerX;
        var movePlayerY;
        
        sound.play("swordSound");

        switch(this.playerWay) {
            case "left":
                if(this.hit) {
                    moveBgY = -24;
                    moveBg = -595;
                    movePlayerX = this.x - 15;
                    movePlayerY = this.y;
                    this.move(movePlayerX, movePlayerY);
                    width = this.width + 15;
                    height = this.height + 15;
                } else {
                    moveBg = -110;
                    moveBgY = -24;
                    movePlayerX = this.x + 15;
                    movePlayerY = this.y;
                    this.move(movePlayerX, movePlayerY);
                    width = this.width - 15;
                    height = this.height - 15;
                }
                break;
            case "right":
                if(this.hit) {
                    moveBgY = -24;
                    moveBg = -528;
                    width = this.width + 15;
                    height = this.height + 15;
                } else {
                    moveBg = -283;
                    moveBgY = -24;
                    width = this.width - 15;
                    height = this.height - 15;
                }
                break;
            case "up":
                if(this.hit) {
                    width = this.width + 15;
                    height = this.height + 15;
                    moveBgY = -5;
                    moveBg = -462;
                    movePlayerX = this.x;
                    movePlayerY = this.y - 15;
                    this.move(movePlayerX, movePlayerY);
                } else {
                    width = this.width - 15;
                    height = this.height - 15;
                    moveBg = -397;
                    moveBgY = -24;
                    movePlayerX = this.x;
                    movePlayerY = this.y + 15;
                    this.move(movePlayerX, movePlayerY);
                }
                break;
            case "down":
                if(this.hit) {
                    width = this.width + 15;
                    height = this.height + 15;
                    moveBgY = -24;
                    moveBg = -668;
                } else {
                    width = this.width - 15;
                    height = this.height - 15;
                    moveBg = -60;
                    moveBgY = -24;
                }
                break;
        }
        this.updateSize(width, height);
        $('#player').css({
            backgroundPosition: moveBg + "px "+moveBgY + "px"
            });
    },

    //Adds a new kill to the kill counter
    newKill: function() {
        this.playerKills = this.playerKills + 1;
        this.mapKills = this.mapKills + 1;
    },

    //Resets the map kill counter
    resetMapKill: function() {
        this.mapKills = 0;
    }


}
